﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL.Entities;
using System.Data.SqlClient;
using System.Data;

namespace DAL
{
    public class FacturarDAL : DBController
    {
        public List<FacturarEntityRes> GetCuponesAFacturar(FacturarEntityReq req)
        {
            try
            {
                List<FacturarEntityRes> listado = new List<FacturarEntityRes>();
                using (SqlConnection conn = this.OpenConn())
                {

                    SqlDataReader dr;

                    SqlCommand dCmd = new SqlCommand("MARAVILLA_SQL.spGetCuponesAFacturar", conn);

                    dCmd.CommandText = "MARAVILLA_SQL.spGetCuponesAFacturar";

                    dCmd.CommandType = CommandType.StoredProcedure;
                    dCmd.Parameters.AddWithValue("@intIdProveedor", req.IntIdProveedor);
                    dCmd.Parameters.AddWithValue("@fechaFin", req.fchhta);
                    dCmd.Parameters.AddWithValue("@fechaInicio", req.fchdde);


                    // dCmd.ExecuteNonQuery();
                    dr = dCmd.ExecuteReader();
                    dCmd.Dispose();
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            FacturarEntityRes item = new FacturarEntityRes();
                            item.BigDNI = long.Parse(dr["bigDNI"].ToString());
                            item.FloatPrecio_Ficticio = float.Parse(dr["floatPrecio_Ficticio"].ToString());
                            item.DatFecha_Compra = Convert.ToDateTime(dr["datFecha_Compra"]);
                            item.DatFecha_Entregado = Convert.ToDateTime(dr["datFecha_Entregado"]);
                            item.IntIdCompra = int.Parse(dr["intIdCompra"].ToString());
                            item.VarCodigo_Groupon = dr["varCodigo_Groupon"].ToString();

                            listado.Add(item);
                        }
                    }
                }
                return listado;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public CrearFacturarEntity AddFactura(CrearFacturarEntity req)
        {
            try
            {
                CrearFacturarEntity fact = new CrearFacturarEntity();
                using (SqlConnection conn = this.OpenConn())
                {

                    SqlDataReader dr;

                    SqlCommand dCmd = new SqlCommand("MARAVILLA_SQL.spAddFactura", conn);

                    dCmd.CommandText = "MARAVILLA_SQL.spAddFactura";

                    dCmd.CommandType = CommandType.StoredProcedure;

                    // dCmd.ExecuteNonQuery();
                    dr = dCmd.ExecuteReader();
                    dCmd.Dispose();
                    if (dr.HasRows)
                    {
                        dr.Read();
                        fact.IntIdFactura = int.Parse(dr["NUMEROFACTURA"].ToString());

                    }
                }
                return fact;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }



        public CrearFacturarEntity FacturarCompra(CrearFacturarEntity req)
        {
            try
            {
                CrearFacturarEntity fact = new CrearFacturarEntity();
                using (SqlConnection conn = this.OpenConn())
                {


                    SqlCommand dCmd = new SqlCommand("MARAVILLA_SQL.spFacturarCompra", conn);

                    dCmd.CommandText = "MARAVILLA_SQL.spFacturarCompra";

                    dCmd.CommandType = CommandType.StoredProcedure;
                    dCmd.Parameters.AddWithValue("@intIdDetalleFactura", req.IntIdFactura);
                    dCmd.Parameters.AddWithValue("@intIdCompra", req.IntIdCompra);

                    // dCmd.ExecuteNonQuery();
                    dCmd.ExecuteNonQuery();
                    dCmd.Dispose();
                    
                }
                return req;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
